﻿using System;
using System.Collections.Generic;
using System.Linq;
using Epam.Exceptions.BusinessLogicExceptions;
using Epam.Exceptions.RepositoryExceptions;
using Epam.PeopleInterfaces;
using Epam.PeopleModel;


namespace Epam.BusinessLogic
{
    public class NewsService:INewsService
    {
        private readonly INewsRepository _repository;

        private readonly INewsSkipAlgoritm _algoritm;

        public NewsService(INewsRepository repository,INewsSkipAlgoritm algoritm)
        {
            _repository = repository;
            _algoritm = algoritm;
        }

        public void Create(INewsEntity item)
        {
            try
            {
                _repository.Create(item);
            }
            catch (CrudOperationException e)
            {
                throw new DatabaseException(e.Message);
            }
        }

        public void Update(INewsEntity item)
        {
            try
            {
               _repository.Update(item);
            }
            catch (CrudOperationException e)
            {
                throw new DatabaseException(e.Message);
            }
            
        }

        public void Delete(Guid id)
        {
           try
           {
               _repository.Delete(id);
           }
           catch (CrudOperationException e)
           {
               throw new DatabaseException(e.Message);
           }
        }

        public INewsEntity GetById(Guid id)
        {
            try
            {
                return NewsMapper.Convert(_repository.GetById(id));
            }
            catch (FindOperationException e)
            {
                throw new DatabaseException(e.Message);
            }
            
        }

        public ICollection<INewsEntity> ReadAll()
        {
            try
            {
                return (from obj in _repository.ReadAll()
                        select NewsMapper.Convert(obj)).ToList();
            }
            catch (FindOperationException e)
            {
                throw new DatabaseException(e.Message);
            }
           
        }

        public ICollection<INewsEntity> ReadByPage(int currentPage, int itemPerPage)
        {
            try
            {
                return (from obj in _repository.ReadPage(currentPage, itemPerPage)
                        select NewsMapper.Convert(obj)).ToList();
            }
            catch (FindOperationException e)
            {
                throw new DatabaseException(e.Message);
            }
            
        }

        public ICollection<IViewNewsEntity> ReadAllView()
        {
            try
            {
                return (from obj in _repository.ReadAll()
                        select NewsMapper.ConvertToViewEntity(obj, _algoritm)).ToList();
            }
            catch (FindOperationException e)
            {
                throw new DatabaseException(e.Message);
            }
           
        }

        public ICollection<IViewNewsEntity> ReadViewByPage(int currentPage, int itemPerPage)
        {
            try
            {
                return (from obj in _repository.ReadPage(currentPage, itemPerPage)
                        select NewsMapper.ConvertToViewEntity(obj, _algoritm)).ToList();
            }
            catch (FindOperationException e)
            {
                throw new DatabaseException(e.Message);
            }
           
        }

        public INewsEntity NewEntity(string text, string title)
        {
            return new NewsEntity(Guid.NewGuid(),text,title,DateTime.Now);
        }
    }
}
